﻿using Jupiter.Infrastructure.Exceptions;
using Jupiter.Infrastructure.Permission.Attributes;
using Jupiter.Infrastructure.Responses;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Wips.AppService.Publisher;
using Wips.AppService.Publisher.AgentServerPermissions;
using Wips.AppService.Publisher.AgentServerPermissions.Requests;
using Wips.AppService.Publisher.AgentServerPermissions.Responses;
using Wips.AppService.Publisher.Models;
using Wips.AppService.Publisher.Requests;
using Wips.Infrastructure.Model;

namespace Wips.MainServer.Controllers
{
    /// <summary>
    /// 子服务器服务
    /// </summary>
    public class AgentServerController : CustomeBaseController
    {
        private readonly IAgentServerAppService _agentServerAppService;

        private readonly IMediator _mediator;

        private readonly IAgentServerPermissionAppService _agentServerPermissionAppService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="agentServerAppService"></param>
        /// <param name="mediator"></param>
        public AgentServerController(IAgentServerAppService agentServerAppService, IMediator mediator, IAgentServerPermissionAppService agentServerPermissionAppService)
        {
            _agentServerAppService = agentServerAppService;
            _mediator = mediator;
            _agentServerPermissionAppService = agentServerPermissionAppService;
        }


        /// <summary>
        /// 获取注册的服务
        /// </summary>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<PageResult<AgentServerModel>> GetPagesAsync([FromBody] GetAgentServerPageRequest request)
        {
            return await _agentServerAppService.GetPageAsync(request);
        }



        /// <summary>
        /// 获取单条子服务器信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="CoreException"></exception>
        [Permission]
        [HttpGet]
        public async Task<AgentServerConfigModel> GetByIdAsync([FromQuery] string id)
        {
            return await _agentServerAppService.GetByIdAsync(id);
        }


        /// <summary>
        /// 获取健康状态
        /// </summary>
        /// <returns></returns>
        [Permission]
        [HttpGet]
        public async Task<bool> GetHealthyAsync([FromQuery] string id)
        {
            return await _agentServerAppService.CheckHealthyAsync(id);
        }


        /// <summary>
        /// 更新配置
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPut]
        public async Task<string> UpdateAsync([FromBody] UpdateAgentServerRequest request)
        {
            return await _mediator.Send(request);
        }


        /// <summary>
        /// 获取子服务的配置
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<PageResult<AgentServerConfigModel>> GetAgentServerConfigsAsync([FromBody] GetAgentServerConfigPageRequest request)
        {

            return await _agentServerAppService.GetAgentServerConfigsAsync(request);
        }

        /// <summary>
        /// 添加/更新子服务配置
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<string> AddOrUpdateAgentConfig(AddOrEditAgentServerConfigRequest request)
        {
            return await _mediator.Send(request);
        }


        /// <summary>
        /// 获取子服务器的盘符
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="CoreException"></exception>
        [Permission]
        [HttpGet]
        public async Task<List<string>> GetAgentRootPath([FromQuery] string id)
        {
            return await _agentServerAppService.GetAgentRootPath(id);
        }
        /// <summary>
        /// 获取子服务器的文件夹 by目录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        /// <exception cref="CoreException"></exception>
        [Permission]
        [HttpGet]
        public async Task<List<DirectoryModel>> GetDirectoryAsync([FromQuery] string id, [FromQuery] string path)
        {

            return await _agentServerAppService.GetDirectoryAsync(id, path);
        }

        /// <summary>
        /// 执行备份
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="CoreException"></exception>
        [Permission]
        [HttpPost]
        public async Task<string> BackupAsync([FromBody] BackupRequest request)
        {
            return await _agentServerAppService.BackupAsync(request);
        }




        /// <summary>
        /// 获取级联数据
        /// </summary>
        /// <returns></returns>
        [Permission]
        [HttpGet]
        public async Task<List<CascaderModel>> GetCascaderAsync()
        {
            return await _agentServerAppService.GetCascaderAsync();
        }

        /// <summary>
        /// 获取我的服务器授权列表
        /// </summary>
        /// <returns></returns>
        [Permission]
        [HttpGet]
        public async Task<GetPermissionResponse> GetPermissionListAsync(string? userId)
        {
            return await _agentServerPermissionAppService.GetPermissionListAsync(userId);
        }

        /// <summary>
        /// 保存授权
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<string> SavePermissionAsync([FromBody] SaveAgentPermissionRequest request)
        {
            return await _mediator.Send(request);
        }
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<List<string>> AgentRunScriptAsync([FromBody] AgentRunScriptRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 获取文件文本内容
        /// </summary>
        /// <param name="id"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        /// <exception cref="CoreException"></exception>
        [Permission]
        [HttpGet]
        public async Task<string> GetFileTextAsync(string id, string path)
        {
            return await _agentServerAppService.GetFileTextAsync(id, path);
        }

        /// <summary>
        /// 保存文件文本
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<string> AgentSaveFileTextAsync([FromBody] AgentSaveFileTextRequest request)
        {
            return await _mediator.Send(request);
        }

    }
}
